/* NOTICE
    OpenJaWS - Open Java Weather Station
    
    Copyright (C) 2008 Grant Gardner <grant@lastweekend.com.au>
    
    OpenJaWS is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
NOTICE */

package au.com.lastweekend.openjaws.units;

public enum WindUnit implements Unit {

    METRES_PER_SECOND(SpeedUnit.METRES_PER_SECOND),

    KILOMETRES_PER_HOUR(SpeedUnit.KILOMETRES_PER_HOUR),

    MILES_PER_HOUR(SpeedUnit.MILES_PER_HOUR),

    KNOT(SpeedUnit.KNOT),

    BEAUFORT();

    private SpeedUnit delegate;

    private WindUnit() {

        delegate = null;
    };

    private WindUnit(SpeedUnit speedUnit) {

        delegate = speedUnit;
    }

    public double convertFrom(double value, Unit unit) {

        WindUnit unitFrom = (WindUnit) unit;

        // W = 0.836 B ^ (3/2)
        // 3/2 = logB(W/0.836)
        // 3/2 = log10(W/0.836) / log10(B)
        // 3/2 log10(B) = log10(W/0.836)
        // log10(B) = log10(W/0.836) * 2/3
        // y = log10(X)
        // x= 10^y
        // B = 10 ^ (log10(W/0.836) * 2/3)

        if (unitFrom == BEAUFORT) {
            value = 0.836 * Math.pow(value, 1.5);
            unitFrom = METRES_PER_SECOND;
        }

        if (this == BEAUFORT) {
            value = METRES_PER_SECOND.convertFrom(value, unitFrom);
            return Math.pow(10.0, (Math.log10(value / 0.836) / 1.5));
        }

        return this.delegate.convertFrom(value, unitFrom.delegate);
    }

    public String toHTMLString() {

        return toString();
    }

    public String toString() {

        if (this == BEAUFORT) {
            return "beaufort";
        } else {
            return delegate.toString();
        }
    }

}
